//
//  BottomCollectionView.m
//  HoldingBarrage
//
//  Created by lingji001 on 2018/8/21.
//  Copyright © 2018年 lingji001. All rights reserved.
//

#import "BottomCollectionView.h"
#import "HoldingModel.h"
#import "BottomCollectionViewCell.h"
#import "BottomHeadCollectionReusableView.h"
@interface BottomCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

//@property (nonatomic, strong) ui

/**
 完成按钮
 */
@property (nonatomic, strong) UIButton *completeButton;

/**
 间隔宽度
 */
@property (nonatomic, assign) CGFloat space;

@property (nonatomic, assign) CGFloat width;


@end

@implementation BottomCollectionView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.space = 10;
        self.width = (UIScreen.mainScreen.bounds.size.width - 10 * 7) / 6 - 1;
        [self initUI];
        [self configUI];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


#pragma mark 初始化UI控件
- (void)initUI{
    self.completeButton = [UIButton new];
    [self addSubview:self.completeButton];
    self.completeButton.layer.cornerRadius = self.width / 2;
    self.completeButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.completeButton.layer.borderWidth = 1;
    [self.completeButton setTitle:@"完成" forState:UIControlStateNormal];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.minimumZoomScale = self.space;
    self.collectionView.maximumZoomScale = self.space;
    layout.itemSize = CGSizeMake(self.width, self.width);
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[BottomCollectionViewCell class] forCellWithReuseIdentifier:BottomCollectionViewCellId];
    [self.collectionView registerClass:[BottomHeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:BottomHeadCollectionReusableViewId];
    self.collectionView.allowsMultipleSelection = YES;
}

- (void)setModel:(HoldingModel *)model{
    _model = model;
    [self.collectionView reloadData];
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] animated:NO scrollPosition:(UICollectionViewScrollPositionNone)];
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:1] animated:NO scrollPosition:(UICollectionViewScrollPositionNone)];
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:2] animated:NO scrollPosition:(UICollectionViewScrollPositionNone)];
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3] animated:NO scrollPosition:(UICollectionViewScrollPositionNone)];
}

#pragma mark 为UI控件添加约束
- (void)configUI{
    [self.completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_offset(self.width);
        make.right.inset(self.space);
        make.bottom.equalTo(self.collectionView.mas_top).inset(self.space);
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_offset(20 * 4 + self.space * 8 + self.width * 4);
    }];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(self.space, self.space, self.space, self.space);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(UIScreen.mainScreen.bounds.size.width, 20);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    switch (section) {
        case 0:return self.model.speedArray.count;
        case 1:return self.model.fontSizeArray.count;
        case 2:return self.model.fontNameArray.count;
        case 3:return self.model.colorArray.count;
        default:return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BottomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BottomCollectionViewCellId forIndexPath:indexPath];
    cell.smallView.layer.cornerRadius = self.width / 2;
    switch (indexPath.section) {
        case 0:
        {
            cell.contentLabel.text = self.model.speedArray[indexPath.row];
            cell.smallView.backgroundColor = [UIColor blackColor];
        }
            break;
        case 1:
        {
            cell.contentLabel.text = self.model.fontSizeArray[indexPath.row];
            cell.smallView.backgroundColor = [UIColor blackColor];
        }
            break;
        case 2:
        {
            cell.contentLabel.text = @"样式";
            cell.contentLabel.font = [UIFont fontWithName:self.model.fontNameArray[indexPath.row] size:15];
            cell.smallView.backgroundColor = [UIColor blackColor];
        }
            break;
        case 3:
        {
            cell.contentLabel.text = @"";
            cell.smallView.backgroundColor = self.model.colorArray[indexPath.row];
        }
            break;
            

    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (NSIndexPath *index in collectionView.indexPathsForSelectedItems) {
        if (indexPath.section == index.section && index.row != indexPath.row) {
            [collectionView deselectItemAtIndexPath:index animated:NO];
            [self updateModel:indexPath];
            break;
        }
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        BottomHeadCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:BottomHeadCollectionReusableViewId forIndexPath:indexPath];
        switch (indexPath.section) {
            case 0:view.contentLabel.text = @"速度";break;
            case 1:view.contentLabel.text = @"字号";break;
            case 2:view.contentLabel.text = @"字体";break;
            case 3:view.contentLabel.text = @"颜色";break;
        }
//        view.backgroundColor = [UIColor whiteColor];
        return view;
    }else{
        return nil;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.hidden = YES;
    if (self.block) {
        self.block(nil);
    }
}

#pragma mark 更新数据
- (void)updateModel:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            self.model.speed = self.model.speedArray[indexPath.row];
            break;
        case 1:
            self.model.fontSize = self.model.fontSizeArray[indexPath.row];
            break;
        case 2:
            self.model.fontName = self.model.fontNameArray[indexPath.row];
            break;
        case 3:
            self.model.color = self.model.colorArray[indexPath.row];
            break;
            
        default:
            break;
    }
    if (self.block) {
        self.block(self.model);
    }
}

@end
